Function pointer as argument in C
Till now, we have seen that in C programming, we can pass the variables as an argument to a function. We cannot pass the function as an argument to another function. But we can pass the reference of a function as a parameter by using a function pointer. This process is known as call by reference as the function parameter is passed as a pointer that holds the address of arguments. If any change made by the function using pointers, then it will also reflect the changes at the address of the passed variable.Therefore, C programming allows you to create a pointer pointing to the function, which can be further passed as an argument to the function. We can create a function pointer as follows:
(type) (*pointer_name)(parameter);
Let's consider an example:
loat (*add)(); // this is a legal declaration for the function pointer
float *add(); // this is an illegal declaration for the function pointer
float add (int a, int b); // function declaration
float (*a)(int, int); // declaration of a pointer to a function
a=add; // assigning address of add() to 'a' pointer
Now, 'a' is a pointer pointing to the add() function. We can call the add() function by using the pointer, i.e., 'a'. Let's see how we can do that:
a(2, 3);
Let's see a simple example of how we can pass the function pointer as a parameter.
void display(void (*p)())
{
for(int i=1;i<=5;i++)
{
p(i);
}
}
void print_numbers(int num)
{
cout<
- We have defined two functions named 'display()' and print_numbers().
- Inside the main() method, we have declared a function pointer named as (*p), and we call the display() function in which we pass the print_numbers() function.
- When the control goes to the display() function, then pointer *p contains the address of print_numbers() function. It means that we can call the print_numbers() function using function pointer *p.
- In the definition of display() function, we have defined a 'for' loop, and inside the for loop, we call the print_numbers() function using statement p(i). Here, p(i) means that print_numbers() function will be called on each iteration of i, and the value of 'i' gets printed.
Output
#include
#include
#include
int compare(const int *p, const int *q);
int (*f)(const void *a, const void *b);
int main()
{
int a[]={4,7,6,1,3,2};
int num=sizeof(a)/sizeof(int);
f=&compare;
qsort(a, num, sizeof(int), (*f));
for(int i=0;i
- We have defined an array of integer type. After creating an array, we have calculated the size of an array by using the sizeof() operator, and stores the size in the num
- We define a compare() function, which compares all the elements in an array and arranges them in ascending order.
- We also have declared the function pointer, i.e., (*f), and stores the address of compare() function in (*f) by using the statement f=&compare.
- We call qsort() function in which we pass the array, size of the array, size of the element, and the comparison function. The comparison function, i.e., compare() will compare the array elements until the elements in an array get sorted in ascending order.